//
//  HHTabBarViewController.m
//  Flower
//
//  Created by 花花 on 2016/12/10.
//  Copyright © 2016年 花花. All rights reserved.
//

#import "HHBaseTabBarViewController.h"
#import "HHBaseNavViewController.h"
#import "ThemeLabel.h"
#import "ThemeButton.h"
#import "ThemeImageView.h"
#import "HHCenterClassViewController.h"



@interface HHBaseTabBarViewController (){

    ThemeImageView *selectItem;
    ThemeImageView *backgroundView;
}

@end

@implementation HHBaseTabBarViewController

#pragma mark - Life Cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createChildControllers]; //创建所有子控制器
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self removeUITabBarButton];
    
}
#pragma mark - removeUITabBarButton
-(void)removeUITabBarButton{
    
    for (UIView *view in self.tabBar.subviews) {
        if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [view removeFromSuperview];
        }
    }
    
}
#pragma mark - createChildControllers
-(void)createChildControllers{
    NSArray *controllers =@[@"HHControllsClassViewController",@"HHFunctionClassViewController",@"HHCenterClassViewController",
                            @"HHCocoClassViewController",@"HHMoreClassViewController"];
    
    NSArray *imagArr = @[ @"home_tab_icon_1@2x.png",@"home_tab_icon_2@2x.png",@"home_tab_icon_3@2x.png",
                          @"home_tab_icon_4@2x.png",  @"home_tab_icon_5@2x.png"];
    
    NSArray *titles = @[@"控件分类",@"功能分类",@"😑",@"Coco的🏠",@"更多"];
    
    CGFloat itemW = screen_width/controllers.count;
    
    ThemeImageView *img =[[ThemeImageView alloc]initWithFrame:CGRectMake(0, -6, screen_width, 55)];
    img.imageName = @"mask_navbar@2x.png";
    [self.tabBar addSubview:img];
    selectItem =[[ThemeImageView alloc]initWithFrame:CGRectMake(0, 2, itemW, 45)];
    selectItem.imageName =  @"home_bottom_tab_arrow@2x.png";
    [self.tabBar addSubview:selectItem];
    for (int i = 0 ; i<controllers.count; i++) {
        
        ThemeButton *btn =[[ThemeButton alloc]initWithFrame:CGRectMake(itemW*i, 2, itemW, 45)];
        btn.imageName = imagArr[i];
        [btn addTarget:self action:@selector(selectbtnAction:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = 100 +i;
        [self.tabBar addSubview:btn];
        
        NSString *className =controllers[i];
        Class class =NSClassFromString(className);
        UIViewController *vc =[class new];
        vc.title = titles[i];
        
        HHBaseNavViewController *nav =[[HHBaseNavViewController alloc]initWithRootViewController:vc];
        [self addChildViewController:nav];
    }
}

#pragma mark - SEL Method
-(void)selectbtnAction:(UIButton *)btn{
    self.selectedIndex = btn.tag - 100;
    [UIView animateWithDuration:0.3 animations:^{
        selectItem.center = btn.center;
    }];
}

@end

